{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-30T14:18:56.205473Z",
     "start_time": "2019-07-30T14:18:55.051617Z"
    }
   },
   "outputs": [],
   "source": [
    "from trectools import TrecRun, TrecEval, TrecQrel, fusion\n",
    "\n",
    "r1 = TrecRun(\"./robust03/runs/input.aplrob03a.gz\")\n",
    "r2 = TrecRun(\"./robust03/runs/input.UIUC03Rd1.gz\")\n",
    "\n",
    "# Easy way to create new baselines by fusing existing runs:\n",
    "fused_run = fusion.reciprocal_rank_fusion([r1,r2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-30T14:18:56.935808Z",
     "start_time": "2019-07-30T14:18:56.207618Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P@25 -- Run 1: 0.339, Run 2: 0.287, Fusion Run: 0.344\n"
     ]
    }
   ],
   "source": [
    "qrels_file =  \"./robust03/qrel/robust03_qrels.txt\"\n",
    "qrels = TrecQrel(qrels_file)\n",
    "\n",
    "r1_p25 = TrecEval(r1, qrels).get_precision(depth=25)          # P@25: 0.3392\n",
    "r2_p25 = TrecEval(r2, qrels).get_precision(depth=25)          # P@25: 0.2872\n",
    "fused_run_p25 = TrecEval(fused_run, qrels).get_precision(depth=25)   # P@25: 0.3436\n",
    "\n",
    "print(\"P@25 -- Run 1: %.3f, Run 2: %.3f, Fusion Run: %.3f\" % (r1_p25, r2_p25, fused_run_p25))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-30T14:18:57.845994Z",
     "start_time": "2019-07-30T14:18:56.937736Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File my_fused_run.txt writen.\n"
     ]
    }
   ],
   "source": [
    "# Save run to disk with all its topics\n",
    "fused_run.print_subset(\"my_fused_run.txt\", topics=fused_run.topics())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
